package com.wuad.platformwuad.module.study.threadDemo.ExecutorServiceDemo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 多线程
 */
class MyRunnable implements Runnable  {

    @Override
    public void run() {
        int i=0;
        for (;i<20; i++) {
            System.out.println("i = " + i);
            if(i==15){
                ExecutorService pool = Executors.newFixedThreadPool(2);
                pool.shutdown();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

class MyRunnable1 implements Runnable  {

    @Override
    public void run() {
        for (int i=0; i<30; i++) {
            System.out.println("y = " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

public class ExecutorServiceDemo {
    public static void main(String[] args) {
        // 创建一个线程池对象，控制要创建几个线程对象。
        // public static ExecutorService newFixedThreadPool(int nThreads)
        ExecutorService pool = Executors.newFixedThreadPool(2);
        // 可以执行Runnable对象或者Callable对象代表的线程
        Future<?> bl = pool.submit(new MyRunnable());
        System.out.println(bl);
        pool.submit(new MyRunnable1());

        //结束线程池
        pool.shutdown();
    }
}
